

#include "Library.h"

//TOWN

void Inn(PLAYER*);
void Armory(PLAYER*);
void ItemShop(PLAYER*);

bool Town(PLAYER* player)
{
	int choice = 0;

	while (choice != 5)
	{
		cout << "You stand in the middle of the town.\n";
		cout << "All around you there are people going about their daily lives.\n";
		cout << "To your left you spot an INN and to your right you spot the ARMORY and ITEM SHOP\n\n";

		Wait();

		cout << "What would you like to do?\n";
		cout << "1: Enter INN\n";
		cout << "2: Enter ARMORY\n";
		cout << "3: Enter ITEM SHOP\n";
		cout << "4: Check Stats\n";
		cout << "5: Leave TOWN\n";

		cout << ">";

		cin >> choice;
		cin.clear();
		cin.ignore(INT_MAX,'\n');

		cout << endl;

		switch (choice)
		{
		case 1:
			{
				cout << "You walk into the inn.\n\n";
				Wait();
				Inn(player);
				break;
			}
		case 2:
			{
				cout << "You walk towards the Armory\n\n";
				Wait();
				Armory(player);
				break;
			}
		case 3:
			{
				ItemShop(player);
				break;
			}
		case 4:
			{
				DisplayStats(player);
				break;
			}
		default: break;
		}
	}

	return true;
}

void Inn(PLAYER* player)
{
	cout << "You stand in an almost vacant inn. Except for an old man standing behind \n";
	cout << "a counter, there is no one else currently staying here.\n\n";
	cout << "You walk over to the old man. He grins, delighted to see a visitor.\n";
	cout << "Hello and welcome. Would you like to stay in my inn for only 10 gold coins?\n";

	char answer;
	answer = 'm';

	while (true)
	{
		cout << "Stay? (10 gold) y/n\n";
		cout << ">";
		cin >> answer;
		cin.ignore(10,'\n');

		if (answer == 'y' || answer == 'n')
			break;
	}

	cout << endl;

	if (answer == 'y')
	{
		if (player->GetGold() >= 10)
		{
			player->SpendGold(10);
			cout << "You find a nice bed and fall fast asleep.\n";
			Wait();
			cout << "\n(HP and MP restored)\n\n";
			player->SetHealth(player->GetMaxHealth());
			player->SetMana(player->GetMaxMana());
			cout << "HP: " << player->GetHealth() << " / " << player->GetMaxHealth() << " ";
			cout << "MP: " << player->GetMana() << " / " << player->GetMaxMana() << "\n" ;
			
			Wait();
		}
		else
		{
			cout << "It looks like you don't have enough gold.\n";
			Wait();
			answer = 'n';
		}
	}

	if (answer == 'n')
	{
		cout << "You turn around and walk out of the inn.\n";
		Wait();
	}
}


void Armory(PLAYER* player)
{
	cout << "You stand in the entrance of the armory. All along the walls are various\n";
	cout << "weapons and armor. You can hear the blacksmith forging somewhere\n";
	cout << "behind the armory. To your left stands the armory merchant.\n\n";

	cout << "MERCHANT: Interested in anything?\n";
	Wait();

	int answer = 0;

	while (true)
	{
		cout << "1: Buy Weapons\n";
		cout << "2: Buy Armor\n";
		cout << "3: Leave\n\n";

		cout << ">";
		cin >> answer;
		cin.clear();
		cin.ignore(INT_MAX,'\n');

		cout << "\n\n";


		if (answer == 1)
		{
			//display weapons
			WEAPON* weaponlist = new WEAPON[7];

			weaponlist[0].CreateWeapon("Dagger",4,15);
			weaponlist[1].CreateWeapon("Hatchet",8,200);
			weaponlist[2].CreateWeapon("Bronze Sword",15,1000);
			weaponlist[3].CreateWeapon("Battle Axe",30,10000);
			weaponlist[4].CreateWeapon("Great Sword",50,20000);
			weaponlist[5].CreateWeapon("Master Sword",70,50000);
			weaponlist[6].CreateWeapon("XCalibur",150,150000);

			cout << "Weapons" << endl;

			for (int loop=0;loop < 7;loop++)
			{

				cout << loop+1 << ": " << weaponlist[loop].GetName() << " - DMG: " << weaponlist[loop].GetDamage() << ", Price: " << weaponlist[loop].GetPrice() << endl;
			}

			cout << endl << "0: Cancel\n\n";

			int answer = -1;
			cout << "Which weapon would you like to purchase?\n";

			while (true)
			{
				cout << ">";
				cin >> answer;
				cin.clear();
				cin.ignore(INT_MAX,'\n');

				if (answer < 8 && answer > 0)
				{
					//check if the player can purchase the item
					if (player->GetGold() > weaponlist[answer-1].GetPrice())
					{
						//player can afford
						player->SpendGold(weaponlist[answer-1].GetPrice());
						player->SetWeapon(weaponlist[answer-1]);

						cout << "You now have the " << player->GetWeapon()->GetName() << ".\n";
						Wait();
						cout << endl;
						break;
					}
					else
					{
						//player cant afford
						cout << "You don't have enough money.\n";
						Wait();
						cout << endl;
						break;
					}
				}

				if (answer == 0)
				{
				//bitches
					break;
				}

			}

			delete [] weaponlist;
		} // end if buy weapon
		else if (answer == 2)
		{
			//display armor;
			ARMOR* armorlist = new ARMOR[5];

			armorlist[0].CreateArmor("Cloth",1,10);
			armorlist[1].CreateArmor("Leather",3,150);
			armorlist[2].CreateArmor("Chain Mail",5,400);
			armorlist[3].CreateArmor("Plate",10,1500);
			armorlist[4].CreateArmor("Dragon Scale",20,10000);

			cout << "Armor" << endl;

			for (int loop=0;loop < 5;loop++)
			{

				cout << loop+1 << ": " << armorlist[loop].GetName() << " - DMG: " << armorlist[loop].GetAP() << ", Price: " << armorlist[loop].GetPrice() << endl;
			}

			cout << endl << "0: Cancel\n\n";

			int answer = -1;
			cout << "Which armor would you like to purchase?\n";

			while (true)
			{
				cout << ">";
				cin >> answer;
				cin.clear();
				cin.ignore(INT_MAX,'\n');

				if (answer < 6 && answer > 0)
				{
					//check if the player can purchase the item
					if (player->GetGold() > armorlist[answer-1].GetPrice())
					{
						//player can afford
						player->SpendGold(armorlist[answer-1].GetPrice());
						player->SetArmor(armorlist[answer-1]);

						cout << "You now have the " << player->GetArmor()->GetName() << ".\n";
						Wait();
						cout << endl;
						break;
					}
					else
					{
						//player cant afford
						cout << "You don't have enough money.\n";
						Wait();
						cout << endl;
						break;
					}
				}

				if (answer == 0)
				{
					break;
				}

			}

			delete [] armorlist;
		}
		else if (answer == 3)
		{
			cout << "You turn around and leave the store.\n";
			Wait();
			break;
		}
		else
		{
			cout << "Invalid Command.\n";
		}
	} // end while in shop
}

/// ITEM SHOP

void ItemShop(PLAYER* player)
{																						//
	cout << "You stand in the item shop. Assorted potions fill the countertops and scrolls\n";
	cout << "lay tacked to the walls.\n\n";
	
	int answer = 0;
	while (true)
	{
		cout << "1: Buy Potions\n";
		cout << "2: Buy Spells\n";
		cout << "3: Leave\n\n";

		cout << ">";
		cin >> answer;
		cin.clear();
		cin.ignore(INT_MAX,'\n');

		if (answer == 1)
		{
			//buy potions
			POTION* potionlist = new POTION[10];

			//health potions
			potionlist[0].CreateHealthPotion("Small Health Potion", 10,5);
			potionlist[1].CreateHealthPotion("Medium Health Potion", 25,50);
			potionlist[2].CreateHealthPotion("Large Health Potion", 75,500);
			potionlist[3].CreateHealthPotion("Elixer", 200,5000);
			potionlist[4].CreateHealthPotion("Grand Elixer", 500,15000);
			potionlist[5].CreateHealthPotion("Elixer of Life", player->GetMaxHealth(),50000);

			//mana potions
			potionlist[6].CreateManaPotion("Small Mana Potion",10,20);
			potionlist[7].CreateManaPotion("Medium Mana Potion",25,50);
			potionlist[8].CreateManaPotion("Large Mana Potion",40,500);
			potionlist[9].CreateManaPotion("Grand Mana Potion",60,5000);

			cout << "Potions" << endl;

			for (int loop=0;loop < 10;loop++)
			{

				cout << loop+1 << ": " << potionlist[loop].GetName() << " - Health: " << potionlist[loop].GetHealthRestore() << ", Mana: " << potionlist[loop].GetManaRestore() << ", Price: " << potionlist[loop].GetPrice() << endl;
			}

			cout << endl << "0: Cancel\n\n";

			int answer = -1;
			cout << "Which potion would you like to purchase?\n";

			while (true)
			{
				cout << ">";
				cin >> answer;
				cin.clear();
			cin.ignore(INT_MAX,'\n');

				if (answer < 11 && answer > 0)
				{
					//check if the player can purchase the item
					if (player->GetGold() > potionlist[answer-1].GetPrice())
					{
						//player can afford
						player->SpendGold(potionlist[answer-1].GetPrice());
						//Give potion
						player->AddPotion(&potionlist[answer-1]);

						cout << "You purchased a " << potionlist[answer-1].GetName() << ".\n";
						Wait();
						cout << endl;
						break;
					}
					else
					{
						//player cant afford
						cout << "You don't have enough money.\n";
						Wait();
						cout << endl;
						break;
					}
				}

				if (answer == 0)
				{
					break;
				}

			}

			delete [] potionlist;
		}
		else if (answer == 2)
		{
			//buy spells

			SPELL* slist[10];

			for (int clear=0;clear < 10;clear++)
				slist[clear] = NULL;

			slist[0] = new FIREBALL();
			slist[1] = new HEAL();
			slist[2] = new POISON();
			slist[3] = new RANDOM();
			slist[4] = new BLAZE();

			cout << "Spells" << endl;

			for (int loop=0;loop < 10;loop++)
			{
				if (slist[loop] != NULL)
					cout << loop+1 << ": " << slist[loop]->GetName() << ", Mana: " << slist[loop]->GetManaCost() << ", Price: " << slist[loop]->GetPrice() << endl;
			}

			cout << endl << "0: Cancel\n\n";

			int answer = -1;
			cout << "Which spell would you like to purchase?\n";

			while (true)
			{
				cout << ">";
				cin >> answer;
				cin.clear();
			cin.ignore(INT_MAX,'\n');

				if (answer < 11 && answer > 0)
				{
					//check if the player can purchase the item
					if (player->GetGold() > slist[answer-1]->GetPrice())
					{
						//player can afford
						player->SpendGold(slist[answer-1]->GetPrice());
						//Give spell
						player->AddSpell(slist[answer-1]);

						cout << "You purchased the spell " << slist[answer-1]->GetName() << ".\n";
						Wait();
						cout << endl;
						break;
					}
					else
					{
						//player cant afford
						cout << "You don't have enough money.\n";
						Wait();
						cout << endl;
						break;
					}
				}

				if (answer == 0)
				{
					break;
				}

			}

			for (int del=0;del < 10;del++)
			{
				if (slist[del] != NULL)
					delete slist[del];
			}

		}
		else if (answer == 3)
		{
			cout << "You turn around and walk out of the shop.\n";
			Wait();
			cout << endl;
			break;
		}
		else
		{
			cout << "Invalid command.\n";
		}

		cout << "\n";
	} // while in item shop
}



